using Distributions, Interpolations, Optim, HCubature, QuantEcon, Printf, PlotlyJS, Dates, JLD2, LinearAlgebra, ProgressBars

include("ctRoot.jl")
include("cs.jl")

function replicate(infolder="Savefiles/", folder="Output/"; saveall=true, slides=false)

    qual = ifelse(slides, "_slides", "")

    mt, dk = load(infolder * "/mt.jld2", "mt", "dk")
    mtp = load(infolder * "/mtpsi.jld2", "mt_psi")

    fig1_Ramsey = comp_plot_planner(mt; slides, k=5, Average=false, Kambe=false, title="", share=true)
    saveall && savefig(fig1_Ramsey, folder * "fig1" * qual * ".pdf", width=850, height=350)

    fig2_types = plot_announcements(; slides)
    saveall && savefig(fig2_types, folder * "fig2" * qual * ".pdf", width=900, height=450)


    fig3_devs = hplot(mt.ct; share=true, slides, yaxis_range=[0, 0.5])
    saveall && savefig(fig3_devs, folder * "fig3" * qual * ".pdf", width=900, height=350)

    fig4_L = Lplot(mt.ct; slides)
    saveall && savefig(fig4_L, folder * "fig4" * qual * ".pdf", width=900, height=450)

    fig5_cred = Cplot(mt; slides, share=true)
    saveall && savefig(fig5_cred, folder * "fig5" * qual * ".pdf", width=900, height=450)

    fig6_plans = planspwide(mt, dk; slides, share=true)
    saveall && savefig(fig6_plans, folder * "fig6" * qual * ".pdf", width=900, height=400)

    # Comparative statics
    fig7 = plot_consol(:σ; share=true, slides, input = infolder)
    saveall && savefig(fig7, folder * "fig7" * qual * ".pdf", width=900, height=400)

    fig8a = plot_consol(:β; slides, input = infolder)
    saveall && savefig(fig8a, folder * "fig8a" * qual * ".pdf", width=500, height=350)

    fig8b = plot_consol(:κ; slides, input = infolder)
    saveall && savefig(fig8b, folder * "fig8b" * qual * ".pdf", width=500, height=350)

    fig9a = twolines(mtp, jp=2; slides)
    saveall && savefig(fig9a, folder * "fig9a" * qual * ".pdf", width=500, height=350)

    fig9b = twolines(mtp, show="C", jp=2; slides)
    saveall && savefig(fig9b, folder * "fig9b" * qual * ".pdf", width=500, height=350)

    fig10 = implied_plan_wide(mtp; slides)
    saveall && savefig(fig10, folder * "fig10" * qual * ".pdf", width=900, height=350)

    mth = load(infolder * "/mt_highres.jld2", "mt")
    find_equil!(mth, 1e-13)
    fig11a = strategy_μ(mth; slides, save_stats=true, folder, censor=1e-9)[1]
    saveall && savefig(fig11a, folder * "fig11" * qual * ".pdf", width=900, height=400)


    # Appendix


    fig12_app = Eplot(mt.ct; slides)
    saveall && savefig(fig12_app, folder * "fig12" * qual * ".pdf", width=900, height=400)


    fig13_app = Lωplot(mt; slides, share=true)
    saveall && savefig(fig13_app, folder * "fig13" * qual * ".pdf", width=900, height=500)


    fig14_app = Lplot_fixed_ω(mt; slides, share = true)
    saveall && savefig(fig14_app, folder * "fig14" * qual * ".pdf", width=900, height=400)

    fig15_app = planspwide(mt; slides, share=true)
    saveall && savefig(fig15_app, folder * "fig15" * qual * ".pdf", width=900, height=400)

    fig16 = plot_cs(:σ; showLmat=true, folder = "Savefiles/CS", slides)
    saveall && savefig(fig16, folder * "fig16" * qual * ".pdf", width=900, height=400)

    nothing
end